﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity.Design.PluralizationServices;
using System.Globalization;
using AbstraX.TypeMappings;
using System.Collections;

namespace AbstraX
{
    public static class GeneratorExtensions
    {
        public static string CamelCase(this string text)
        {
            return text.Substring(0, 1).ToLower() + text.Substring(1, text.Length - 1);
        }

        public static bool IsPlural(this string text)
        {
            var service = PluralizationService.CreateService(new CultureInfo("en-US"));

            return text == service.Pluralize(text);
        }

        public static string Pluralize(this string text)
        {
            var service = PluralizationService.CreateService(new CultureInfo("en-US"));

            return service.Pluralize(text);
        }

        public static string CamelCasePluralize(this string text)
        {
            return text.Pluralize().CamelCase();
        }

        public static string XmlTypeToDotNet(string type)
        {
            return TypeMapper.ObjectMappings[type].MappedFromType;
        }

        public static string RemoveQuotes(this string text)
        {
            if (text.StartsWith("\"") || text.StartsWith("'"))
            {
                text = text.Remove(0, 1);
            }

            if (text.EndsWith("\"") || text.EndsWith("'"))
            {
                text = text.Remove(text.Length - 1);
            }

            return text;
        }

        public static string ToCommaDelimitedList(this IEnumerable list)
        {
            var text = string.Empty;
            var count = list.Cast<object>().Count();

            for (var x = 0; x < count; x++)
            {
                var item = list.Cast<object>().ElementAt(x).ToString();

                if (x < count - 1)
                {
                    item += ", ";
                }

                text += item;
            }

            return text;
        }

        public static string ToCommaDelimitedList(this IEnumerable list, Func<string, string> postProcess)
        {
            var text = string.Empty;
            var count = list.Cast<object>().Count();

            for (var x = 0; x < count; x++)
            {
                var item = list.Cast<object>().ElementAt(x).ToString();

                if (x < count - 1)
                {
                    item += ",";
                }

                text += postProcess(item);
            }

            return text;
        }

        public static string ToCommaDelimitedList(this IEnumerable list, Func<string, bool, string> postProcess)
        {
            var text = string.Empty;
            var count = list.Cast<object>().Count();
            var isLast = false;

            for (var x = 0; x < count; x++)
            {
                var item = list.Cast<object>().ElementAt(x).ToString();

                if (x < count - 1)
                {
                    item += ",";
                }
                else
                {
                    isLast = true;
                }

                text += postProcess(item, isLast);
            }

            return text;
        }
    }
}
